#encoding=utf8
from sklearn.metrics import accuracy_score
import pandas as pd
import numpy as np
from student import load_dataset, Classifier

def calculate_accuracy(file_name, classifier):
    test_dataset, test_labels = load_dataset(file_name)
    random_indices = np.random.permutation(test_dataset.shape[0])
    test_dataset = test_dataset[random_indices,:]
    test_labels = test_labels[random_indices,:]
    predicted_labels = classifier.predict(test_dataset)
    if isinstance(predicted_labels, np.ndarray):
        if predicted_labels.size != test_labels.size:
            print('错误：输出的标签数量与测试集大小不一致')
            accuracy = 0
        else:
            accuracy = np.mean(predicted_labels.flatten()==test_labels.flatten())
    else:
        print('错误：输出格式有误，必须为ndarray格式')
        accuracy = 0
    return accuracy

if __name__ == '__main__':

    with open('./step1/student.py', 'r', encoding='UTF-8') as f:
        code = f.read()
        if 'print' in code:
            print('禁止查看测试数据')
        else:
            classifier = Classifier()
            classifier.train()

            sum_accuracies = 0
            num_test_datasets = 0
            accuracy = calculate_accuracy('./step1/test_dataset_clean.pkl', classifier)
            print(f'你在干净的测试数据集上的正确率为：{accuracy:.4f}')
            sum_accuracies += accuracy
            num_test_datasets += 1
            for noise in range(1,7,1):
                for level in range(1,4,1):
                    accuracy = calculate_accuracy('./step1/test_dataset_noise_type'+str(noise)+'_level'+str(level)+'.pkl', classifier)
                    print(f'你在噪声类型{noise}等级{level}的测试数据集上的正确率为：{accuracy:.4f}')
                    sum_accuracies += accuracy
                    num_test_datasets += 1
            mean_accuracies = sum_accuracies/num_test_datasets
            print(f'你在总共{num_test_datasets}个测试集上的平均正确率为：{mean_accuracies:.4f}')



